package org.example.RabbitMq.Service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.example.Constant.RabbitConstant;
import org.example.Dto.AdminDto;
import org.example.RabbitMq.Service.UserService;
import org.example.RabbitMq.amqpService.TableCacheSender;
import org.example.mapper.UserMapper;
import org.example.pojos.Admin;
import org.example.utils.JsonUtil;
import org.example.utils.batchCreateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author : zhangbo
 * @data : 2024/7/12
 * @description :
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,Admin> implements UserService {
    @Resource
    private TableCacheSender tableCacheSender;
    @Resource
    private UserMapper userMapper;

    @Override
    public List<Admin> findByPhone(String phone) {
        if (!StringUtils.isNotBlank(phone)) {
            log.info("电话号码为空！");
            return null;
        }
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getPhone, phone);
        List<Admin> adminList = userMapper.selectList(wrapper);
        if (CollUtil.isEmpty(adminList)) {
            log.info("未查询到用户");
            return new ArrayList<>();
        }
        try {
            log.info("开始推送消息到MQ");
            tableCacheSender.sendResetMsg(RabbitConstant.Queue.QUEUE_USER, adminList, RabbitConstant.RoutingKey.ROUTE_USER);
            log.info("推送消息到MQ成功");
        } catch (Exception e) {
            log.info("消息发送MQ异常", e);
        }
        return adminList;
    }

    @Override
    public List<Admin> findByList(List<String> ids) {
        if (!CollUtil.isNotEmpty(ids)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getId, ids);
        List<Admin> adminList = userMapper.selectList(wrapper);
        List<Admin> dtoList = adminList.stream().map(admin -> {
            Admin dto=new Admin();
            dto.setStatus("1");
            return dto;
        }).collect(Collectors.toList());
        try {
            log.info("开始推送消息到MQ"+RabbitConstant.Queue.QUEUE_USER);
            tableCacheSender.sendResetMsg(RabbitConstant.Queue.QUEUE_USER, dtoList, RabbitConstant.RoutingKey.ROUTE_USER);
            log.info("推送消息到MQ成功"+RabbitConstant.Queue.QUEUE_USER);
        } catch (Exception e) {
            log.info("消息发送MQ异常");
        }
        return null;
    }

    @Override
    public String insertBatch() throws ParseException {
        try {
            //随机插入数据
            LocalTime localTime = LocalTime.now();
            int num = Integer.parseInt(localTime.toString().replace(":", "").replace(".", "").substring(6, 8));
            List<Admin> list = new ArrayList<>();
            for (int i = 0; i < num; i++) {
                Admin admin=new Admin();
                AdminDto dto = batchCreateUtils.insert();
                BeanUtils.copyProperties(dto,admin,new String[] {"id"});
                userMapper.insert(admin);
            }
        } catch (NumberFormatException e) {
            log.info("批量随机插入异常");
            return "0";
        }
        return "1";
    }

    @Override
    public String changeStatus(Date time) {
        String flag="1";
        try {
            //查询loginTime早于2020-01-01 00:00:00的用户，异步修改status
            LambdaQueryWrapper<Admin>wrapper=new LambdaQueryWrapper<>();
            wrapper.lt(Admin::getLoginTime,time);
            List<Admin> list = userMapper.selectList(wrapper);
            list.stream().forEach(dto->{
                dto.setStatus("3");
            });
            //异步更新推出时间和修改人
            log.info("开始推送消息到MQ"+RabbitConstant.Queue.QUEUE_UPDATE_STATUS);
            tableCacheSender.sendResetMsg(RabbitConstant.Queue.QUEUE_UPDATE_STATUS,list,RabbitConstant.RoutingKey.ROUTE_UPDATE_STATUS);
            log.info("推送消息到MQ成功"+RabbitConstant.Queue.QUEUE_UPDATE_STATUS);
        } catch (Exception e) {
            log.info("修改状态失败");
            flag="0";
            return flag;
        }
        return flag;
    }

}
